home *** CD-ROM | disk | FTP | other *** search
-
- var picnikScreenGrab = {
- onLoad: function()
- {
- // check if this firefox has canvas AND toDataURL
- var has_canvas = true;
- try
- {
- var canvas = document.createElementNS('http://www.w3.org/1999/xhtml','canvas');
- if( canvas == null )
- throw Exception;
- if( !("toDataURL" in canvas) )
- throw Exception;
- }
- catch(e)
- {
- has_canvas = false;
- }
-
- if( !has_canvas )
- {
- var e = document.getElementById("picnik-tool-menu");
- if(e)
- e.hidden=true;
-
- e = document.getElementById("picnik-button");
- if(e)
- e.hidden=true;
-
- e = document.getElementById("picnik-ctx-grab");
- if(e)
- e.hidden=true;
- }
-
- },
-
- grabFull: function()
- {
- var h = 0;
- var w = 0;
- if( window.content.document.compatMode == "CSS1Compat" )
- {
- h = window.content.document.documentElement.scrollHeight;
- w = window.content.document.documentElement.scrollWidth;
- }
- else
- {
- h = window.content.document.body.scrollHeight;
- w = window.content.document.body.scrollWidth;
- }
- this.grab(window.content,0,0,w,h);
- },
-
- grabVisible: function()
- {
- var x = window.content.scrollX;
- var y = window.content.scrollY;
- var h = 0;
- var w = 0;
- if( window.content.document.compatMode == "CSS1Compat" )
- {
- h = window.content.document.documentElement.clientHeight;
- w = window.content.document.documentElement.clientWidth;
- }
- else
- {
- h = window.content.document.body.clientHeight;
- w = window.content.document.body.clientWidth;
- }
-
- this.grab(window.content,x,y,w,h);
- },
-
- grab: function(win, x, y, w, h)
- {
-
- // Figure out how big to make the canvas
- const MAX_DIM = 2800;
- var canvasW = w;
- var canvasH = h;
- if( w>MAX_DIM || h>MAX_DIM)
- {
- if(w > h)
- {
- canvasW = MAX_DIM;
- canvasH = canvasW*h/w;
- }
- else
- {
- canvasH = MAX_DIM;
- canvasW = canvasH*w/h;
- }
- }
-
- // Make the canvas
- var canvas = document.createElementNS('http://www.w3.org/1999/xhtml','canvas');
- canvas.style.width = canvasW+"px";
- canvas.style.height = canvasH+"px";
- canvas.width = canvasW;
- canvas.height = canvasH;
- var ctx = canvas.getContext("2d");
- ctx.clearRect(0, 0, canvasW, canvasH);
- ctx.save();
- ctx.scale(canvasW/w, canvasH/h);
- ctx.drawWindow(win, x, y, w, h, "rgb(255,255,255)");
- ctx.restore();
-
- // Save it to picnik
- this.saveDataURL(canvas.toDataURL("image/png", ""), "image/png");
- },
-
- showGears: function()
- {
- var doc = window.content.document
- var bod = doc.getElementsByTagName('body')[0];
- var shadow = doc.createElement('div');
- shadow.id = 'picnik-overlay-shadow';
- shadow.style.opacity = "0.7"
- shadow.style.MozOpacity = shadow.style.opacity
- shadow.style.top="0"
- shadow.style.left="0"
- shadow.style.width="100%"
- shadow.style.height="100%"
- shadow.style.display="block"
- shadow.style.position="fixed"
- shadow.style.backgroundColor="#000";
- shadow.style.zIndex = "9997"
- bod.appendChild(shadow);
-
- var gears_img = doc.createElement('img');
- gears_img.src = "chrome://picnik/content/gears.gif";
-
- var gears = doc.createElement('div');
- gears.id = 'picnik-overlay-gears';
- gears.style.top="50%"
- gears.style.left="50%"
- gears.style.marginTop = -gears_img.height/2 + "px";
- gears.style.marginLeft = -gears_img.width/2 + "px";
- gears.style.display="block"
- gears.style.position="fixed"
- // gears.style.backgroundColor="#ccc";
- gears.style.zIndex = "9998"
- gears.appendChild(gears_img)
- bod.appendChild(gears);
-
- return {body: bod, div_shadow: shadow, div_gears: gears}
- },
-
- hideGears: function(gears)
- {
- gears.body.removeChild( gears.div_shadow )
- gears.body.removeChild( gears.div_gears )
- },
-
- saveDataURL: function(url, mimeType)
- {
- const MULTI = "@mozilla.org/io/multiplex-input-stream;1";
- const STRINGIS = "@mozilla.org/io/string-input-stream;1";
- const IOSVC = "@mozilla.org/network/io-service;1"
- const nsIMultiplexInputStream = Components.interfaces.nsIMultiplexInputStream;
- const nsIStringInputStream = Components.interfaces.nsIStringInputStream;
- const nsIIOService = Components.interfaces.nsIIOService
- const BOUNDARY = "345823569845694578678";
-
- var gears = this.showGears()
-
- // Build the mime header
- var header = new String();
- header += "\r\n";
- header += "--" + BOUNDARY + "\r\n";
- header += "Content-disposition: form-data;name=\"_returntype\"\r\n\r\ntext\r\n";
- header += "--" + BOUNDARY + "\r\n";
- header += "Content-disposition: form-data;name=\"_apikey\"\r\n\r\n"+picnikCommon.APIKey+"\r\n";
- header += "--" + BOUNDARY + "\r\n";
- header += "Content-disposition: form-data;name=\"_file\";filename=\"snapshot.png\"\r\n";
- header += "Content-Type: "+mimeType+"\r\n";
- header += "\r\n";
- var header_stream = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
- header_stream.setData(header, header.length);
-
- // create a data url from the canvas and then create a stream
- var io = Components.classes[IOSVC].getService(nsIIOService);
- var channel = io.newChannelFromURI( io.newURI(url, "UTF8", null) );
- var data_stream = channel.open();
-
- // Ending MIME boundary
- var end_stream = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
- var bs = new String("\r\n--" + BOUNDARY + "--\r\n");
- end_stream.setData(bs, bs.length);
-
- // Stick 'em all together
- var mux_stream = Components.classes[MULTI].createInstance(nsIMultiplexInputStream);
- mux_stream.appendStream(header_stream);
- mux_stream.appendStream(data_stream);
- mux_stream.appendStream(end_stream);
-
- // Finally, POST it to picnik!
- var req = new XMLHttpRequest();
- req.open('POST', picnikCommon.baseURL+"service", true);
- req.onreadystatechange = function(){ picnikScreenGrab.requestState(req, gears); };
- req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
- req.setRequestHeader("Content-Length", mux_stream.available() );
- req.send(mux_stream);
- },
-
- requestState: function(req, gears)
- {
- if( req.readyState == 4 )
- {
- if( req.status == 200 )
- {
- result = req.responseText;
- gBrowser.selectedTab = gBrowser.addTab(result);
- //gBrowser.addTab(result);
- }
- else
- {
- alert('There was a problem with the request\nstatus=' + req.status);
- }
- picnikScreenGrab.hideGears(gears)
- }
- }
- };
-
- window.addEventListener("load", picnikScreenGrab.onLoad, false);
-